%creates the ampl_combined.dat file
%needs trainingdata, testdata, N, Ntrain (generally from the twostep_controlmethod.m)
filename = ['ampl_combined.dat'];
fid = fopen(filename, 'w'); 
fprintf(fid, '#dat file generated through matlab script\n#ampl_combined.dat\n');
fprintf(fid, 'param C1 := 1;\n');
fprintf(fid, 'param C2 := 2; #CAUTION CHECK THESE TWO!!\n');
fprintf(fid, 'param Ntrain := %d;\n',Ntrain);
fprintf(fid, 'param dimLambda := 3;\n');
fprintf(fid, 'param Ntest := 6;\n');
fprintf(fid, 'param d : 1 2 3 4 5 6 :=\n');
fprintf(fid, '1 %s\n',num2str(C(1,:)));
fprintf(fid, '2 %s\n',num2str(C(2,:)));
fprintf(fid, '3 %s\n',num2str(C(3,:)));
fprintf(fid, '4 %s\n',num2str(C(4,:)));
fprintf(fid, '5 %s\n',num2str(C(5,:)));
fprintf(fid, '6 %s;\n',num2str(C(6,:)));
fprintf(fid, '#param prob : 1      2         3         4         5         6:=\n');
fprintf(fid, '#1         1         1         1         1         1         1;\n');
fprintf(fid, '#1    0.9241    0.9299    0.9579    0.1353    0.1061    0.5000;\n'); 
fclose(fid);

%processing training and test data
trainingdataind2write = 1:Ntrain;
trainingdata2write = [trainingdataind2write' ones(Ntrain,1) trainingdata(:,1:2)];
traininglabels2write = [trainingdataind2write' trainingdata(:,3)];
testdataind2write=1:N;
testdata2write = [testdataind2write' ones(N,1) testdata(:,1:2)];

%appending data to ampl dat file
fid = fopen(filename, 'a'); fprintf(fid, 'param: ytrain :=\n'); fclose(fid);
dlmwrite(filename ,traininglabels2write(1:Ntrain-1,:), '-append','delimiter',' ');
fid = fopen(filename, 'a'); fprintf(fid, '%s;\nparam xtrain: 1 2 3 :=\n',num2str(traininglabels2write(Ntrain,:))); fclose(fid);
dlmwrite(filename ,trainingdata2write(1:Ntrain-1,:), '-append','delimiter',' ');
fid = fopen(filename, 'a'); fprintf(fid, '%s;\nparam xtest: 1 2 3 :=\n',num2str(trainingdata2write(Ntrain,:))); fclose(fid);
dlmwrite(filename ,testdata2write(1:N-1,:), '-append','delimiter',' ');
fid = fopen(filename, 'a'); fprintf(fid, '%s;\n#append ends here---------------------------------------\n',num2str(testdata2write(N,:))); fclose(fid);